package com.example.myapplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText edtTxtMainName;
    private EditText edtTxtMainAge;

    private Button btnMainInsert;
    private Button btnMainUpdate;
    private Button btnMainDelete;
    private Button btnMainQuery;
    private TextView tvMainDisplay;
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;
    private TextView tvMainTitle;
    private Button btnMainQueryOnly;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        openHelper = new MySQLiteOpenHelper(this, "student.db", null, 1);
        btnMainInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtTxtMainName.getText().toString().trim();
                int age = Integer.valueOf(edtTxtMainAge.getText().toString().trim());
                //float score = Float.valueOf(edtTxtMainScore.getText().toString().trim());
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("age", age);
                //values.put("score", score);
                database = openHelper.getWritableDatabase();
                long result = database.insert("users", null, values);
                database.close();
                Log.i("TAG", "onClick:" + result);
            }
        });
        btnMainQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                database = openHelper.getReadableDatabase();
                Cursor cursor = database.query("users", null, null, null, null, null, null);

                if (cursor.moveToFirst()) {
                    tvMainDisplay.setText(null);
                    do {
                        String name = cursor.getString(1);
                        int age = cursor.getInt(2);
                        //float score = cursor.getFloat(3);
                        tvMainDisplay.append(name + "  " + age + "  " + "\n");
                    } while (cursor.moveToNext());
                }

                cursor.close();
                database.close();
            }
        });

        btnMainQueryOnly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                database = openHelper.getReadableDatabase();
                Cursor cursor = database.query("users", null, null, null, null, null, null);



                if (cursor.moveToFirst()) {
                    tvMainDisplay.setText(null);
                    String n = edtTxtMainName.getText().toString();
                    do {
                        if(n.equals(cursor.getString(1))){
                            String name = cursor.getString(1);
                            int age = cursor.getInt(2);
                            //float score = cursor.getFloat(3);
                            tvMainDisplay.append(name + "  " + age + "  ");
                        }

                    } while (cursor.moveToNext());
                }



                cursor.close();
                database.close();
            }
        });


        btnMainUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtTxtMainName.getText().toString().trim();
                int age = Integer.valueOf(edtTxtMainAge.getText().toString().trim());
                ContentValues values = new ContentValues();
                values.put("age", age);
                database = openHelper.getWritableDatabase();
                database.update("users", values, "name=?", new String[]{name});
                database.close();
            }
        });

        btnMainDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtTxtMainName.getText().toString().trim();
                database = openHelper.getWritableDatabase();
                database.delete("users", "name=?", new String[]{name});
                database.close();
            }
        });

    }

    private void initView() {
        edtTxtMainName = (EditText) findViewById(R.id.edtTxtMainName);
        edtTxtMainAge = (EditText) findViewById(R.id.edtTxtMainAge);

        btnMainInsert = (Button) findViewById(R.id.btnMainInsert);
        btnMainUpdate = (Button) findViewById(R.id.btnMainUpdate);
        btnMainDelete = (Button) findViewById(R.id.btnMainDelete);
        btnMainQuery = (Button) findViewById(R.id.btnMainQuery);
        tvMainDisplay = (TextView) findViewById(R.id.tvMainDisplay);
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        btnMainQueryOnly = (Button) findViewById(R.id.btnMainQueryOnly);
    }
}